package controller

import (
	"ginblog/enum"
	"ginblog/model"
	"ginblog/resp"
	"ginblog/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type ArticleController struct {
	article service.ArticleService
	r       resp.Resp
}

type ArticleConInterface interface {
	AddArticle(c *gin.Context)
	GetArticleById(c *gin.Context)
	GetCateArt(c *gin.Context)
	GetArticles(c *gin.Context)
	EditArticle(c *gin.Context)
	DeleteArticle(c *gin.Context)
}

// AddArticle 添加文章
func (arti *ArticleController) AddArticle(c *gin.Context) {
	// 获取id
	username := c.Param("username")

	// 获取用户传递得到数据
	article := model.Article{}
	err := c.ShouldBind(&article)

	if err != nil {
		arti.r.RespData(enum.ERROR, enum.ERROR.String(), "数据格式有误！", c)
		return
	}
	if article.Title == "" && article.Content == "" && article.Cid == 0 {
		arti.r.RespData(enum.ERROR, enum.ERROR.String(), "标题或内容不能为空！", c)
		return
	}

	// 添加文章到数据库
	code := arti.article.CreateArticle(&article, username)
	if code != enum.SUCCESS {
		arti.r.RespData(code, code.String(), "", c)
		return
	}

	arti.r.RespData(code, code.String(), "文章添加成功！", c)
}

// GetArticleById 查询单个文章
func (arti *ArticleController) GetArticleById(c *gin.Context) {
	// 获取id
	cateId, _ := strconv.Atoi(c.Param("id"))

	article, username := arti.article.GetArticleById(cateId)
	c.JSON(http.StatusOK, gin.H{
		"status":   enum.SUCCESS,
		"message":  enum.SUCCESS.String(),
		"data":     article,
		"username": username,
	})
}

// GetCateArt 查询分类下的所以文章
func (arti *ArticleController) GetCateArt(c *gin.Context) {
	// 获取id
	cateId, _ := strconv.Atoi(c.Param("id"))
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	pageNum, _ := strconv.Atoi(c.Query("pageNum"))
	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	// 判断数据
	if pageNum == 0 {
		pageNum = -1
	}

	art, count := arti.article.GetCateArt(cateId, pageSize, pageNum)
	c.JSON(http.StatusOK, gin.H{
		"status":  enum.SUCCESS,
		"message": enum.SUCCESS.String(),
		"data":    art,
		"total":   count,
	})
}

// GetArticles 查询文章列表
func (arti *ArticleController) GetArticles(c *gin.Context) {
	// 获取用户传输的数据
	// 获取前端传过来的数据 并转换给int类型
	title := c.Query("title")
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	pageNum, _ := strconv.Atoi(c.Query("pageNum"))

	// 判断数据
	if pageNum == 0 {
		pageNum = -1
	}

	articlevo, count := arti.article.GetArticles(title, pageSize, pageNum)

	c.JSON(http.StatusOK, gin.H{
		"status":  enum.SUCCESS,
		"message": enum.SUCCESS.String(),
		"data":    articlevo,
		"total":   count,
	})
}

// EditArticle 编辑文章
func (arti *ArticleController) EditArticle(c *gin.Context) {
	// 获取前端传递的参数
	// 获取id
	articleId, _ := strconv.Atoi(c.Param("id"))

	// 获取修改的数据
	article := model.Article{}
	err := c.ShouldBind(&article)
	if err != nil {
		arti.r.RespData(enum.ERROR, enum.ERROR.String(), "数据格式有误！", c)
	}
	// 修改文章内容
	code := arti.article.EditArticle(articleId, &article)
	if code != enum.SUCCESS {
		arti.r.RespData(code, code.String(), "", c)
		return
	}
	arti.r.RespData(code, code.String(), "修改成功！", c)
}

// DeleteArticle 删除文章
func (arti *ArticleController) DeleteArticle(c *gin.Context) {
	// 获取前端传递的参数
	// 获取id
	userId, _ := strconv.Atoi(c.Param("id"))

	code := arti.article.DeleteArticle(userId)

	if code != enum.SUCCESS {
		arti.r.RespData(code, code.String(), "", c)
		return
	}
	arti.r.RespData(code, code.String(), "删除成功！", c)
}
